(function () {
    var Pagination = require('common:widget/ui/pagination/pagination.js').Pagination,
        util = require('common:widget/ui/libs/util.js').util,
        $page = $('.page-bonus'),
        $recordBlock = $page.find('.record-block'),
        $recordContainer = $recordBlock.find('.record-table tbody');
    P.createPage({
        _data: {
            status: 0
        },

        _create: function () {
            var me = this,
                pagination = new Pagination({
                    container: $page.find('.record-pagination'),
                    perCount: 10,
                    cur: 1,
                    onRefresh: function (curPage, callback) {
                        $.getJSON('/user/getBonusListApi', {
                            status: me.data('status'),
                            pn: curPage
                        }, function (ret, status) {
                            if (ret.error_no == 0 && ret.data) {
                                callback(ret.data.page_count * 10);
                                me._createTableList(ret.data.list);
                            }
                        });
                    }
                });
            this.data({
                'pagination': pagination,
                '$typeTab': $page.find('.type-tab a')
            });
        },

        _init: function () {
            var me = this;
            $('.type-tab li a').click(function () {
                $('.type-tab li a').removeClass('active');
                $(this).addClass('active');
                me.data('status', $(this).attr('data-type'));
                me.refresh();
            });
        },

        //更新数据
        refresh: function () {
            var pagination = this.data("pagination");
            pagination.initPage(1);
        },

        //生成表格内容
        _createTableList: function (list) {
            $recordContainer.html((function () {
                var html = "";
                $.each(list, function (k, v) {
                    html += '' +
                    '<tr>' +
                    '<td>' + v.id + '</td>' +
                    '<td>' + P.couponMap[v.type] + '</td>' +
                    '<td>' + (v.amount ? '￥' + mathUtil.formatAmount(v.amount) : '') + '</td>' +
                    '<td>截止至' + util.toTime(v.expire_time * 1000, 'yyyy-MM-dd') + '</td>' +
                    '<td>' + (v.type == 2 ? '仅限购买真融宝定期产品，且需超过' + (parseInt(v.amount) == 100 ? 50000 : 30000) + '元才能使用。' : P.couponRuleMap[v.type]) + '</td>' +
                    '<td></td>' +
                    '</tr>' +
                    ''
                });
                return html;
            })());
        }
    });
})();